package com.simon.hse.activity.education;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.ExaminationDetailOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 考试详情
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_examination_detail)
public class ExaminationDetailActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_examin_detail_title)
    TextView mTv_examin_detail_title;

    @ViewInject(R.id.tv_examin_detail_time)
    TextView mTv_examin_detail_time;

    @ViewInject(R.id.tv_examin_detail_score)
    TextView mTv_examin_detail_score;

    @ViewInject(R.id.tv_examin_detail_right_count)
    TextView mTv_examin_detail_right_count;

    @ViewInject(R.id.tv_examin_detail_wrong_count)
    TextView mTv_examin_detail_wrong_count;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<ExaminationDetailOutput.DataBean.ErrorQuestionsBean> mDataBeanList = new ArrayList<>();
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mTv_common_title.setText("考试详情");
        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        id = bundle.getString("ID");

        initRecyclerView();
        initExaminDetail();
    }

    @Event(value = {R.id.iv_common_left, R.id.tv_examin_detail_continue})
    private void back(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.tv_examin_detail_continue:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(ExaminationDetailActivity.this, OnlineExaminationActivity.class);
                bundle.putString("TITLE", "在线考试");
                bundle.putString("TYPE", "1");
                bundle.putBoolean("CONTINUE", true);
                bundle.putString("ID", id);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    private void initRecyclerView() {
        LinearLayoutManager llm = new LinearLayoutManager(ExaminationDetailActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(ExaminationDetailActivity.this, R.layout.layout_item_examin_detail, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<ExaminationDetailOutput.DataBean.ErrorQuestionsBean>() {

            @Override
            public void setItemDatas(CommonViewHolder holder, final ExaminationDetailOutput.DataBean.ErrorQuestionsBean dataBean, int position) {
                TextView tv_examin_error_title = holder.getView(R.id.tv_examin_error_title);
                TextView tv_examin_right_answer = holder.getView(R.id.tv_examin_right_answer);
                RecyclerView mRecyclerView = holder.getView(R.id.common_recyclerview);

                tv_examin_error_title.setText((++position) + "、" + dataBean.getQuestionContent());
                tv_examin_right_answer.setText("正确答案：" + dataBean.getCorrectOption());

                List<ExaminationDetailOutput.DataBean.ErrorQuestionsBean.PXQuestionsOptionsBean> optionsBeanList = dataBean.getPX_QuestionsOptions();

                LinearLayoutManager llm = new LinearLayoutManager(ExaminationDetailActivity.this);
                mRecyclerView.setLayoutManager(llm);
                // 如果Item够简单，高度是确定的，打开FixSize将提高性能
                mRecyclerView.setHasFixedSize(true);
                // 设置Item默认动画，加也行，不加也行
                mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                mRecyclerView.setNestedScrollingEnabled(false);

                CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(ExaminationDetailActivity.this, R.layout.layout_item_examin_detail_second, optionsBeanList);
                mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<ExaminationDetailOutput.DataBean.ErrorQuestionsBean.PXQuestionsOptionsBean>() {
                    @Override
                    public void setItemDatas(CommonViewHolder holder, ExaminationDetailOutput.DataBean.ErrorQuestionsBean.PXQuestionsOptionsBean questionsBean, int position) {
                        CheckBox cb_examin_detail = holder.getView(R.id.cb_examin_detail);
                        TextView tv_examin_msg = holder.getView(R.id.tv_examin_msg);

                        tv_examin_msg.setText(questionsBean.getOptionName() + "、" + questionsBean.getOptionComment());
                        if (dataBean.getLastOption().equals(questionsBean.getOptionName())) {
                            cb_examin_detail.setChecked(true);
                        }
                    }
                });
                mRecyclerView.setAdapter(mCommonEmptyAdapter);

            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * 考试详情
     */
    private void initExaminDetail() {
        Call<String> call = HttpManager.getInstance().requestService().EXAMIN_DETAIL(id);
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ExaminationDetailOutput examinationDetailOutput= new Gson().fromJson(result, ExaminationDetailOutput.class);
                if (examinationDetailOutput.getResult().equals("Success")) {
                    final ExaminationDetailOutput.DataBean dataBean = examinationDetailOutput.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTv_examin_detail_title.setText(dataBean.getNameoftrainingplan());
                            String examinationstarttime = dataBean.getExaminationstarttime();
                            if (examinationstarttime.length() > 16) {
                                examinationstarttime = examinationstarttime.substring(0, 16);
                            }
                            mTv_examin_detail_time.setText(examinationstarttime);
                            mTv_examin_detail_score.setText(dataBean.getExaminationscore() + "");
                            mTv_examin_detail_right_count.setText(dataBean.getOkCount() + "");
                            mTv_examin_detail_wrong_count.setText(dataBean.getErrorCount() + "");

                            List<ExaminationDetailOutput.DataBean.ErrorQuestionsBean> errorQuestionsBeanList = dataBean.getErrorQuestions();
                            mDataBeanList.addAll(errorQuestionsBeanList);
                            mCommonEmptyAdapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    ToastUtils.getInstance().showShortToastBottom(examinationDetailOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

}
